package cn.edu.zzuli.wechat.scheduled;


import cn.edu.zzuli.common.constant.RabbitConstant;
import cn.edu.zzuli.model.entity.User;
import cn.edu.zzuli.wechat.user.mapper.UserMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;


import java.util.List;

@Slf4j
@Component
public class ScheduledClass {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RabbitTemplate template;

    @Scheduled(cron = "0 0 9 * * ? ")
    public void signRemind(){
        List<User> users = userMapper.selectList(new QueryWrapper<User>()
                .select("open_id")
                .eq("sign_in", 1));
        if (users != null) {
            for (User user : users) {
                template.convertAndSend(RabbitConstant.EXCHANGE,RabbitConstant.ROUTING_KEY_SIGN_IN,
                        user.getOpenId());
            }
        }
    }

    @Scheduled(cron = "0 0 0 * * ? ")
    public void signStatus(){
       QueryWrapper wrapper = new QueryWrapper();
       wrapper.eq("sign_in",2);
       User user = new User();
       user.setSignIn(0);
       userMapper.update(user,wrapper);
    }
}
